import os
import sys
import json
from config import settings
import time
import getpass
from utils.auth import auth
from atm.atm import taoba_pay

def main():
    '''
    商城入口
    '''
    username = ''
    shop_cart_list = []
    cost = 0
    while True:
        print(' Welcome to TaoBa Shopping '.center(50, '='))
        print('今日商品')
        print('-'*50)
        with open(os.path.join(settings.DB_DIR, 'product_info.json'), 'r') as f:
            product_info = json.load(f)
        for product in product_info:
            print(str(product_info.index(product)).ljust(3, ' '),
                product['name'].ljust(10, ' '), product['price'])
        print('-'*50)
        choice = input('输入你要购买的商品编号或者[q]退出: ')
        if choice.isdigit():
            choice = int(choice)
            if choice >= 0 and choice < len(product_info):
                if not username:
                    username = input('请输入你的用户名: ')
                    password = getpass.getpass('请输入你的密码: ')
                    if not auth(username=username, password=password, type='shop'):
                        print('输入的用户名或密码错误')
                        username = ''
                        continue
                print('==> [%s %s] 已经加入你的购物车' %
                (product_info[choice]['name'], product_info[choice]['price']))
                shop_cart_list.append(product_info[choice])
                time.sleep(1)
            else:
                print('==> 选择的商品编号不存在')
                time.sleep(1)
        elif choice == 'q':
            if shop_cart_list:
                print('你购买了以下商品:')
                print('-'*50)
                for shop in shop_cart_list:
                    print(shop['name'].ljust(10, ' '), shop['price'])
                    cost += shop['price']
                print('-'*50)
                print('你购买的商品需要支付: %d ' % cost)
                paypass = getpass.getpass('输入你的支付密码: ')
                if auth(username=username, paypass=paypass, type='pay'):
                    user_shop_cart = os.path.join(
                        settings.DB_DIR, username + '_shop_cart.json')
                    if os.path.exists(user_shop_cart):
                        with open(user_shop_cart, 'r') as f:
                            old_shop_cart_list = json.load(f)
                            new_shop_cart_list = old_shop_cart_list + shop_cart_list
                    else:
                        new_shop_cart_list = shop_cart_list
                    with open(user_shop_cart, 'w') as f:
                        json.dump(new_shop_cart_list, f)
                    pay_result = json.loads(taoba_pay(username=username, cost=cost))
                    if pay_result['paycode'] == 0:
                        print('购买成功')
                    elif pay_result['paycode'] == 1:
                        print('ATM支付失败')
                    elif pay_result['paycode'] == 2:
                        print('没有查询到相关的ATM账号信息')
                    else:
                        print('网络错误')
                else:
                    print('付款认证失败')
            else:
                print('你的购物车空空如也')
            print('='*50)
            print('正在退出...')
            exit()
        else:
            print('请输入正确的选项')
